// 数据中心实现
#include "data_center.h"
#include <iostream>
#include <thread>

DataCenter::DataCenter(const std::string& topicAddr, const std::string& topicName) : topicAddr(topicAddr), topicName(topicName) {
    sub = zmq::socket_t(ctx, zmq::socket_type::sub);
    sub.connect(topicAddr.c_str());
    sub.set(zmq::sockopt::subscribe, topicName);
}

DataCenter::~DataCenter() {
    sub.close();
}

void DataCenter::startup() {
    zmq::message_t msgTopicName;
    zmq::message_t msgData;
    while (true) {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        if (!sub.recv(msgTopicName, zmq::recv_flags::none)) {
            std::cerr << "Subscribe: Failed to receive topic name" << std::endl;
            continue;
        }
        std::string topicName_(static_cast<char*>(msgTopicName.data()), msgTopicName.size());
        if (!sub.recv(msgData, zmq::recv_flags::none)) {
            std::cerr << "Subscribe: Failed to receive data" << std::endl;
            continue;
        }
        uint8_t *ptr = reinterpret_cast<uint8_t*>(msgData.data());
        auto signal = std::vector<uint8_t>(ptr, ptr+msgData.size());
        std::cout << "@@@@@ 数据中心: " << signal[0] << ", " << signal[1] << "; !!!!!!!" << std::endl;
    }
}